#include "terrain.h"

using namespace std;

terrain::terrain(int size,float scale)
{
	myShader = Utilities::useShader("grid.shader");

	numVerts = size * 3 * 4;

	points = new float[numVerts * 3];
			
	int count = 0;
	for(int x = 0; x < size; x++)
	{
		points[count] = (float)x * scale;
		points[count + 1] = 0.0f;
		points[count + 2] = 0.0f * scale;

		points[count + 3] = (float)x * scale;
		points[count + 4] = 0.0f;
		points[count + 5] = (float)size * scale;
		count += 6;
	}

	for(int z = 0; z < size; z++)
	{
		points[count] = 0 * scale;
		points[count + 1] = 1.0f;
		points[count + 2] = z * scale;

		points[count + 3] = size * scale;
		points[count + 4] = 1.0f;
		points[count + 5] = z * scale;
		count += 6;
	}


	glGenVertexArrays(1,&vao);
	glBindVertexArray(vao);

	glGenBuffers(1,&vbo);
	glBindBuffer(GL_ARRAY_BUFFER,vbo);
	glBufferData(GL_ARRAY_BUFFER, numVerts * 3 * sizeof(GLfloat),this->points,GL_STATIC_DRAW);
	glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
	glEnableVertexAttribArray(0);

	glBindVertexArray(0);

}

void terrain::renderMe(AlphaMatrixStack *mvm, AlphaMatrix *proj)
{
	glUseProgram(this->myShader);

	mvm->pushMatrix();
	mvm->translate(-32.0f * 50.0f , 0.0f , -32.0f * 50.0f);
	
	GLuint u = glGetUniformLocation(myShader,"MVM");
	glUniformMatrix4fv(u,1,GL_FALSE,mvm->getMatrix().returnPtr());

	u = glGetUniformLocation(myShader,"PROJ");
	glUniformMatrix4fv(u,1,GL_FALSE,proj->returnPtr());
	
	
	glBindVertexArray(vao);

	glDrawArrays(GL_TRIANGLE_STRIP,0,numVerts);

	glBindVertexArray(0);
	mvm->popMatrix();
}